﻿using BL;
using BL.Models;
using HitMapsNew.Controllers;
using HitMapsNew.Models;
using HitMapsNew.Models.EditData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HitMapsNew.Views
{
    [AuthorizeUserOnly]
    public class EditDataController : Controller
    {
        public ActionResult Menus()
        {
            return View(new EditDataModel());
        }

        public ActionResult AddMenu(EditDataModel menu)
        {
            BL.Context.Run((context) =>
            {
                MenuItem parent = context.Menus.Where(m => m.ID == menu.ParnetID).FirstOrDefault();

                if (parent.Children == null) parent.Children = new List<MenuItem>();

                parent.Children.Add(new MenuItem { Name = menu.SubMenuName, Parent = parent, PictureName = menu.PictureName });

                context.SaveChanges();
            });

            return RedirectToAction("Menus", "EditData");
        }


        public ActionResult Locations()
        {
            return View(new EditDataModel());
        }

        [HttpPost]
        public ActionResult AddLocation(HttpPostedFileBase file, AddLocation addLocation)
        {
            BL.Context.Run((context) =>
            {
                string fileName = string.Empty;
                if (Request.Files.Count > 0)
                {

                    try
                    {

                        if (file != null && file.ContentLength > 0)
                        {
                            fileName = Path.GetFileName(file.FileName);
                            fileName = string.Format("{0}{1}", Guid.NewGuid().ToString() , fileName);
                            var imagePath = Path.Combine(Server.MapPath("~/Images/Uploaded/"), fileName);
                            file.SaveAs(imagePath);
                        }
                    }
                    catch (Exception ex)
                    {
                        fileName = string.Empty;
                    }
                }

                MenuItem menu = context.Menus.Where(m => m.ID == addLocation.MenuID).FirstOrDefault();

                var location = new Location
                {
                    Lat = addLocation.Lat,
                    Lng = addLocation.Lng,
                    Details = new LocationDetail
                    {
                        Title = addLocation.Title,
                        Content = addLocation.Content,
                        Url = addLocation.Url,
                        Image = fileName
                    }
                };

                menu.Locations.Add(location);


                context.SaveChanges();
            });

            return RedirectToAction("Locations", "EditData");
        }


        public ActionResult ApproveUsers()
        {
            var context = new BL.Context();

            var unAprrovedUsers = BL.Models.Account.User.GetAllUnapprovedUsers(context);

            return View(new ApproveUsersModel(unAprrovedUsers));
        }

        [HttpPost]
        public ActionResult ApproveUsers(List<UserApprovedGetDataModel> userApproved)
        {
            Context.Run((context) =>
            {
                foreach(var user in userApproved)
                {
                    var dbUser = context.Users.Where(u => u.ID == user.ID).FirstOrDefault();

                    if(dbUser != null)
                    {
                        switch(user.status)
                        {
                            case 1:
                                dbUser.IsAuturize = true;
                                break;
                            case 2:
                                dbUser.IsDeleted = true;
                                break;
                        }
                    }
                }

                context.SaveChanges();
            });

            return ApproveUsers();
        }
    }
}
